home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / program / ixemlsrc.lha / ixemul / library / ix_init.c < prev    next >
C/C++ Source or Header  |  1995-12-23  |  5KB  |  196 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *  Portions Copyright (C) 1994 Rafael W. Luebbert
  5.  *
  6.  *  This library is free software; you can redistribute it and/or
  7.  *  modify it under the terms of the GNU Library General Public
  8.  *  License as published by the Free Software Foundation; either
  9.  *  version 2 of the License, or (at your option) any later version.
  10.  *
  11.  *  This library is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  *  Library General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU Library General Public
  17.  *  License along with this library; if not, write to the Free
  18.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  *  $Id: ix_init.c,v 1.12 1994/07/11 22:16:47 rluebbert Exp $
  21.  *
  22.  *  $Log: ix_init.c,v $
  23.  *  Revision 1.12  1994/07/11  22:16:47  rluebbert
  24.  *  Removed arp.library from source - no longer needed.
  25.  *
  26.  *  Revision 1.11  1994/07/07  15:08:21  rluebbert
  27.  *  10*NOFILE -> NOFILE
  28.  *
  29.  *  Revision 1.10  1994/07/07  15:03:00  rluebbert
  30.  *  Removed ttyport dummy routines
  31.  *
  32.  *  Revision 1.9  1994/07/07  12:21:55  rluebbert
  33.  *  *** empty log message ***
  34.  *
  35.  *  Revision 1.8  1994/07/07  11:44:26  rluebbert
  36.  *  *** empty log message ***
  37.  *
  38.  *  Revision 1.7  1994/06/22  15:31:17  rluebbert
  39.  *  Re-enable global environment variables
  40.  *
  41.  *  Revision 1.6  1994/06/19  15:12:59  rluebbert
  42.  *  *** empty log message ***
  43.  *
  44.  *  Revision 1.4  1992/08/09  20:52:38  amiga
  45.  *  change to new way of remembering open libraries
  46.  *  get prepared to deal with ttys
  47.  *
  48.  *  Revision 1.3  1992/05/22  01:43:46  mwild
  49.  *  initialize buddy allocator
  50.  *
  51.  * Revision 1.2  1992/05/18  00:49:07  mwild
  52.  * changed async mp to be global
  53.  *
  54.  * Revision 1.1  1992/05/14  19:55:40  mwild
  55.  * Initial revision
  56.  *
  57.  */
  58.  
  59. #define KERNEL
  60. #include "ixemul.h"
  61. #include "kprintf.h"
  62.  
  63. #include <exec/memory.h>
  64. #include <dos/var.h>
  65.  
  66. #include <string.h>
  67. #include <unistd.h>
  68.  
  69. /* not changed after the library is initialized */
  70. struct ixemul_base        *ixemulbase = NULL;
  71.  
  72. struct ExecBase            *SysBase = NULL;
  73. struct DosLibrary        *DOSBase = NULL;
  74. struct MathIeeeSingBasBase    *MathIeeeSingBasBase = NULL;
  75. struct MathIeeeDoubBasBase    *MathIeeeDoubBasBase = NULL;
  76. struct MathIeeeDoubTransBase    *MathIeeeDoubTransBase = NULL;
  77.  
  78. /* global port for asynchronous packet notification */
  79. struct MsgPort *ix_async_mp;
  80.  
  81. static struct
  82. {
  83.   void **base;
  84.   char *name;
  85. }
  86. ix_libs[] =
  87. {
  88.   { (void **)&DOSBase, "dos.library" },
  89.   { (void **)&MathIeeeSingBasBase, "mathieeesingbas.library" },
  90.   { (void **)&MathIeeeDoubBasBase, "mathieeedoubbas.library" },
  91.   { (void **)&MathIeeeDoubTransBase, "mathieeedoubtrans.library" },
  92.   { NULL, NULL }
  93. };
  94.  
  95. int open_libraries(void)
  96. {
  97.   int i;
  98.  
  99.   SysBase = *(struct ExecBase **)4;
  100.   for (i = 0; ix_libs[i].base; i++)
  101.     if (!(*(ix_libs[i].base) = (void *)OpenLibrary(ix_libs[i].name, 0)))
  102.       {
  103.     ix_panic("%s required!", ix_libs[i].name);
  104.     return 0;
  105.       }
  106.   return 1;
  107. }
  108.  
  109. void close_libraries(void)
  110. {
  111.   int i;
  112.   
  113.   for (i = 0; ix_libs[i].base; i++)
  114.     if (*ix_libs[i].base)
  115.       CloseLibrary(*ix_libs[i].base);
  116. }
  117.  
  118. struct ixemul_base *ix_init (struct ixemul_base *ixbase)
  119. {
  120.   int i;
  121.   char buf[256];
  122.  
  123.   ixemulbase = ixbase;
  124.   if (!check_hardware())
  125.     return 0;
  126.   if (!open_libraries())
  127.     {
  128.       close_libraries();
  129.       return 0;
  130.     }
  131.  
  132.   ixbase->ix_file_tab = (struct file *)AllocMem (NOFILE * sizeof(struct file), MEMF_PUBLIC | MEMF_CLEAR);
  133.   ixbase->ix_fileNFILE = ixbase->ix_file_tab + NOFILE;
  134.   ixbase->ix_lastf = ixbase->ix_file_tab;
  135.  
  136.   /* Read the GMT offset. This environment variable is 5 bytes long. The
  137.      first 4 form a long that contains the offset in seconds and the fifth
  138.      byte is ignored. */
  139.   if (GetVar("IXGMTOFFSET", buf, 6, GVF_BINARY_VAR) == 5 && IoErr() == 5)
  140.     ix_set_gmt_offset(*((long *)buf));
  141.   else
  142.     ix_set_gmt_offset(0);
  143.  
  144.   if (GetVar(IX_ENV_SETTINGS, buf, sizeof(struct ix_settings) + 1, GVF_BINARY_VAR) == sizeof(struct ix_settings))
  145.     ix_set_settings((struct ix_settings *)buf);
  146.   else
  147.     ix_set_settings(ix_get_default_settings());
  148.  
  149.   /* Set the hostname if the environment variable HOSTNAME exists. */
  150.   if (GetVar("HOSTNAME", buf, 256, 0) > 0)
  151.     sethostname(buf, strlen(buf));
  152.  
  153.   /* initialize the list structures for the allocator */
  154.   init_buddy ();
  155.  
  156.   ix_async_mp = (struct MsgPort *) CreateInterruptPort (0, 0, mp_interrupt, 0);
  157.  
  158.   if (ixbase->ix_file_tab && ix_async_mp)
  159.     {
  160.       InitSemaphore (& ixbase->ix_semaph);
  161.  
  162.       configure_context_switch ();
  163.  
  164.       NewList ((struct List *) &ixbase->ix_socket_list);
  165.  
  166.       for (i = 0; i < IX_NUM_SLEEP_QUEUES; i++)
  167.         NewList ((struct List *) &ixbase->ix_sleep_queues[i]);
  168.  
  169.       return ixbase;
  170.     }
  171.   if (ix_async_mp)
  172.     DeleteInterruptPort (ix_async_mp);
  173.  
  174.   if (ixbase->ix_file_tab)
  175.     FreeMem (ixbase->ix_file_tab, NOFILE * sizeof(struct file));
  176.   else
  177.     ix_panic ("out of memory");
  178.  
  179.   close_libraries();
  180.   
  181.   return 0;
  182. }      
  183.  
  184. void ix_lock_base (void)
  185. {
  186.   u.u_oldmask = u.p_sigmask;
  187.   u.p_sigmask = ~0;
  188.   ObtainSemaphore (& ix.ix_semaph);
  189. }
  190.  
  191. void ix_unlock_base (void)
  192. {
  193.   ReleaseSemaphore (& ix.ix_semaph);
  194.   u.p_sigmask = u.u_oldmask;
  195. }
  196.